package com.nuanshui.heatedloan.task.logging.logback;

import java.util.Map;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.UnsynchronizedAppenderBase;

import com.nuanshui.heatedloan.middleware.rabbitmq.RabbitmqMessageSender;
import com.nuanshui.heatedloan.task.constant.TaskGlobalParams;

public class JobInfoLoggerAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {

	
	private RabbitmqMessageSender rabbitmqMessageSender;
	
	
	public JobInfoLoggerAppender(RabbitmqMessageSender rabbitmqMessageSender) {
		 this.rabbitmqMessageSender = rabbitmqMessageSender;
	}
	@Override
	protected void append(ILoggingEvent eventObject) {
		if(this.rabbitmqMessageSender == null) return ;
		Map<String, String> mdc = eventObject.getMDCPropertyMap();
		if(!mdc.containsKey(TaskGlobalParams.JOB_LOG_CODE_KEY)) return;
		
		JSONEventLayout layout = new JSONEventLayout();
		String logJson = layout.doLayout(eventObject);
		rabbitmqMessageSender.send("job.log.queue", logJson,false);	
	}

}
